"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
import re
import time
import random

import pymysql
import requests
from fake_useragent import UserAgent
from lxml import html

from operating_sql import SQLManager


# 15168

class Weibo:
    def __init__(self):
        self.headers = {
            "User-Agent": UserAgent().ie
        }

        self.url = "https://m.weibo.cn/api/container/getIndex?from[]=page_100505&from[]=page_100505&mod[]=TAB&mod[]=TAB&mod[]=TAB&pids[]=plc_main%3Ffrom%3Dpage_100505&pids[]=plc_main&pids[]=plc_main&jumpfrom=weibocom&containerid=1076035095448260&since_id={}"
        self.sql = SQLManager()
        self.since_id = ""
        self.url_list = []
        self.err = 0

    def get_json(self):
        while 1:
            try:
                json_ = requests.get(self.url.format(self.since_id), headers=self.headers).json()
            except:
                print("正在重试")
                time.sleep(5)
                self.err += 1
                if self.err > 10:
                    print("已超最大重试次数")
                    print(self.url.format(self.since_id))
                    return
                continue
            if not json_["data"]["cards"]:
                print(json_)
                return
            if self.url.format(self.since_id) in self.url_list:
                print("已重复")
                print(len(self.url_list))
                return
            else:
                self.url_list.append(self.url.format(self.since_id))
            time.sleep(random.randint(20, 30) / 10)
            for data in json_["data"]["cards"]:
                if data["card_type"] == 9:
                    if "pics" in data["mblog"].keys():
                        title = pymysql.escape_string(data["mblog"]["raw_text"])
                        if "全文" in title:
                            html_ = requests.get("https://m.weibo.cn/detail/" + data["mblog"]["id"]).text
                            string_html = re.findall('"text": "(.*)"', html_)
                            if string_html:
                                title = string_html[0]
                                par_ = html.fromstring(title)
                                title = "".join(par_.xpath("//text()"))
                        img_list = []
                        for img in data["mblog"]["pics"]:
                            img_list.append(img["url"])
                        img_url = ",".join(img_list)
                        self.ins_sql(title, img_url)
                        print(title)
            # 下一页
            # containerid = json_["data"]["cardlistInfo"]["containerid"]
            try:
                self.since_id = json_["data"]["cardlistInfo"]["since_id"]
            except:
                print("已经爬取完成")
            print(self.url.format(self.since_id))

    def run(self):
        self.get_json()

    def ins_sql(self, title, img_url):
        ins_s = 'insert into weibo(content,img_url) values("%s","%s")' % (title, img_url)
        self.sql.run(ins_s)


if __name__ == '__main__':
    wb = Weibo()
    wb.run()
